/**
 * 
 */
package update;

import java.util.Arrays;

import protocol.CtfActionException;
import utils.Conversions;

/**
 * Represents a single Player Update.
 * @author lilywong
 *
 */
public class PlayerUpdateBody {
	public static final int LENGTH = 6;
	
	/* Fields set */
	int playerId;
	int field;
	int value;
	
	public int getPlayerId() { return playerId; }
	public int getField() { return field; }
	public int getValue() { return value; }
	
	/**
	 * Parses the received byte array. (Client usage)
	 * 
	 * @param response
	 * @throws CtfActionException
	 */
	public PlayerUpdateBody( byte[] response ) throws CtfActionException {
		byte[] playerIdInBytes 	= Arrays.copyOfRange(response, 0, 2);
		byte[] fieldInBytes 	= Arrays.copyOfRange(response, 2, 4);
		byte[] valueInBytes 	= Arrays.copyOfRange(response, 4, 6);
		// convert back to host byte order
		this.playerId 	= Conversions.bytesToInt( playerIdInBytes );
		this.field		= Conversions.bytesToInt( fieldInBytes );
		this.value 		= Conversions.bytesToInt( valueInBytes );
		
		// error
		if ( this.playerId == 0 ) throw new CtfActionException();
	}
}
